package com.example.nongye.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.example.nongye.R;
import com.example.nongye.databinding.ActivityLoginBinding;
import com.example.nongye.utils.KeyboardUtils;
import com.example.nongye.utils.ToastUtils;
import com.jaeger.library.StatusBarUtil;
import com.tencent.mmkv.MMKV;

public class LoginActivity extends AppCompatActivity {

    private ActivityLoginBinding binding;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityLoginBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        StatusBarUtil.setColor(this, getResources().getColor(R.color.white), 0);
        StatusBarUtil.setLightMode(this);
        String phone = MMKV.defaultMMKV().getString("phone", "");
        if (!TextUtils.isEmpty(phone)) {
            binding.tvPhone.setText(phone.substring(0, 3) + " ***** " + phone.substring(phone.length() - 3));
        }
        binding.viewPsw.setOnClickListener(view -> {
            binding.imgKeyboard.setVisibility(View.VISIBLE);
            binding.viewPsw.setVisibility(View.GONE);

        });

        binding.imgKeyboard.setOnClickListener(view ->
                {
                    binding.imgKeyboard.setVisibility(View.INVISIBLE);
                    KeyboardUtils.showSoftInput(view);
                }
        );

        binding.btnBack.setOnClickListener(view -> finish());

        binding.btnLogin.setOnClickListener(view -> {
            if (TextUtils.isEmpty(binding.etPsw.getText())) {
                ToastUtils.showShort("请输入密码");
            } else {
                String password = MMKV.defaultMMKV().getString("password", "");
                if (TextUtils.equals(password, binding.etPsw.getText())) {
                    Intent intent = new Intent(LoginActivity.this, AccountActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    ToastUtils.showShort("密码错误");
                }
            }

        });

    }


}